package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.keys.RedisKeys;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.member.service.IUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserService userService;

    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return !userService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public AJaxResult sendVerifyCode(String phone) {
        AJaxResult aJaxResult = new AJaxResult();
        userService.sendVerifyCode(phone);
        return aJaxResult;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public AJaxResult userRegist(String phone, String nickname, String password, String rpassword,String verifyCode) {
        AJaxResult ajaxResult = new AJaxResult();
        userService.userRegist(phone, nickname,password,rpassword,verifyCode);
        return ajaxResult;
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public AJaxResult userLogin(String username, String password, HttpServletResponse response){
        AJaxResult aJaxResult = new AJaxResult();
        String token = userService.userLogin(username,password);
        Cookie cookie = new Cookie("TOKEN",token);
        cookie.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
        cookie.setPath("/");
        response.addCookie(cookie);
        return aJaxResult;
    }
}
